package com.yyyzzz.servlet;

import com.yyyzzz.bean.Student;
import com.yyyzzz.service.impl.studentServiceimpl;
import com.yyyzzz.service.studentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/studentRegisterServlet")
public class studentRegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       /* request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");*/
        String stuNo = request.getParameter("stuNo");
        String stuPwd = request.getParameter("stuPwd");
        String stuName = request.getParameter("stuName");
        String stuClass = request.getParameter("stuClass");
        String stuMajor = request.getParameter("stuMajor");
        String stuEmail = request.getParameter("stuEmail");
        String stutelphone = request.getParameter("stutelphone");
        HttpSession session =request.getSession();
        if (stuNo == null || "".equals(stuNo.trim())){
            session.setAttribute("message","账户不能为空");
            request.getRequestDispatcher("studentRegister.jsp").forward(request,response);
        }
        if (stuPwd == null || "".equals(stuPwd.trim())){
            session.setAttribute("message","密码不能为空");
            request.getRequestDispatcher("studentRegister.jsp").forward(request,response);

        }
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuPwd(stuPwd);
        student.setStuName(stuName);
        student.setStuClass(stuClass);
        student.setStuMajor(stuMajor);
        student.setStuEmail(stuEmail);
        student.setStutelphone(stutelphone);
        //创建studentservice
        studentService studentservice=new studentServiceimpl();
        boolean isRegister=studentservice.isRegister(student);
        if(isRegister==true){
            response.sendRedirect(request.getContextPath()+"/studentLogin.jsp");
        }else {
            session.setAttribute("message","注册失败");
            response.sendRedirect(request.getContextPath()+"/studentRegister.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

